package com.vote.www.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vote.www.core.page.Pagination;
import com.vote.www.domain.Czjact;
import com.vote.www.domain.Czjadvertcount;
import com.vote.www.domain.Czjmonitor;
import com.vote.www.service.CzjadvertcountService;


@Controller
public class CzjadvertcountController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(CzjadvertcountController.class);
	public CzjadvertcountController() {
	}
 

	@RequestMapping("advertcount_svse")
	public void advertcount_svse(Czjadvertcount czjadvertcount,
			HttpServletRequest request, HttpServletResponse response,
			Integer pageSize,Integer pageNo,
			ModelMap model) throws Exception {
		
		try {
			
				czjadvertcount.setCreatetime(new Date());
				czjadvertcount.setTotals(1);
				czjadvertcountService.insertAdvertcount(czjadvertcount);
				jsonResult(1, "", "", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error", e);
			jsonResult(0, "118", "监听动态url失败", "", response);
		}
		
	}
	
	@RequestMapping("advertcount_list")
	public String advertcount_list(String beginTime, String endTime, Integer type,
			HttpServletRequest request, HttpServletResponse response,
			Integer pageSize,Integer pageNo,
			ModelMap model) throws Exception {
		
		try {
			if(null==pageSize)
				pageSize=20;
			if(null==pageNo)
				pageNo=1;
			
			Map<String, Object> map = new HashMap<String, Object>();
			;
			if (StringUtils.isNotBlank(beginTime)) {
				map.put("beginTime", beginTime);
				map.put("createTime", 11);
				if (StringUtils.isBlank(endTime))
					map.put("endTime", new Date());
				else
					map.put("endTime", endTime);
			}

			Pagination pagination = czjadvertcountService
					.getCzjadvertcount(map,pageSize,pageNo);
			model.addAttribute("pagination", pagination);
			if (StringUtils.isNotBlank(beginTime)) {
				model.addAttribute("beginTime", beginTime);
			}
			if (StringUtils.isNotBlank(endTime)) {
				model.addAttribute("endTime", endTime);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "advertcount_list";
	}
	
	@RequestMapping("advertcount_detail")
	public String advertcount_detail(String beginTime, String endTime, String url,
			HttpServletRequest request, HttpServletResponse response,
			Integer pageSize,Integer pageNo,
			ModelMap model) throws Exception {
		
		try {
			if(null==pageSize)
				pageSize=20;
			if(null==pageNo)
				pageNo=1;
			
			Map<String, Object> map = new HashMap<String, Object>();
			
			if(StringUtils.isNotBlank(url)){
				map.put("url", url);
				model.addAttribute("url", url);
			}
		/*	if (StringUtils.isNotBlank(beginTime)) {
				map.put("beginTime", beginTime);
				map.put("createTime", 11);
				if (StringUtils.isBlank(endTime))
					map.put("endTime", new Date());
				else
					map.put("endTime", endTime);
			}
*/
			Pagination pagination = czjadvertcountService
					.getCzjadvertcountByurl(map,pageSize,pageNo);
			model.addAttribute("pagination", pagination);
			/*if (StringUtils.isNotBlank(beginTime)) {
				model.addAttribute("beginTime", beginTime);
			}
			if (StringUtils.isNotBlank(endTime)) {
				model.addAttribute("endTime", endTime);
			}*/

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "advertcount_detail";
	}
	
	
	@Autowired
	private CzjadvertcountService czjadvertcountService;

}